<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\user\Withdraw as WithdrawModel;
use app\common\model\User;
use think\Db;
use think\Config;
use fast\Random;

/**
 * 用户提现接口
 */
class Withdraw extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     *
     */
    public function index()
    {
        $this->success('', ['welcome' => '123']);
    }

    /**
     * 提现列表
     */
    public function withList(){
        
        $user_info = $this->auth->getUserinfo();

        $where['user_id'] = $user_info['id'];  //允许展示
        $field = 'id, type, money, createtime, finishtime, status';
        $list = WithdrawModel::field($field)
            ->where($where)
            ->order('id DESC')
            ->paginate();

        $this->success('提现列表', $list);
    }

    /**
     * 获取提现详情
     * @throws \think\exception\DbException
     */
    public function withInfo(){
        $id = input('id/d', 0);
        if($id == 0){
            $this->error('参数有误');
        }
        $where['id'] = $id;
        $info = WithdrawModel::where($where)->find();
        
        if(!$info){
            $this->error('数据不存在');
        }

        $this->success('提现详情', $info);
    }

    /*
     * 提现金额参数
     */
    public function withConfig(){
        $conf = config('site.withdraw_arr');
        $conf_key = array_keys($conf);
        $conf_value = array_values($conf);

        $return = array(
            'with_key'=>$conf_key,
            'with_value'=>$conf_value
        );

        $this->success('提现金额', $return);
    }
   
    /**
     * 申请提现
     */
    public function addWith(){

        $user_info = $this->auth->getUserinfo();
        $rmb_rate = config('site.rmb_rate');    //兑换比例
        $money_rmb = round($user_info['money']/$rmb_rate, 2);   //民币金额
        //$withdraw_limit = config('site.withdraw_limit');
        $data = $this->request->post();

        $conf = config('site.withdraw_arr');
        $conf_key = array_keys($conf);
        $conf_value = array_values($conf);

        if(!in_array($data['money'], $conf_key)){
            $this->error('提现金额有误');
        }

        $gold = intval($data['money'] * $rmb_rate); //提现对应金币数量
        if($user_info['money'] < $gold){
            $this->error('金币数量不足');   
        }

        /*if($gold < ($withdraw_limit * $rmb_rate)){
            $this->error('最低提现额度：'.$withdraw_limit.'元');
        }*/

        $where['user_id'] = $user_info['id'];
        $today_start = strtotime(date('Y-m-d'));
        $today_end = strtotime(date('Y-m-d').'+1 day');
        $is_with = WithdrawModel::where($where)
            ->where('createtime', ['>=', $today_start], ['<', $today_end], 'and')
            ->count();
        if($is_with > 0){
            $this->error('每天最多提现1次');   
        }

        unset($data['token']);
        if(isset($data['share_from'])) unset($data['share_from']);
        $data['user_id'] = $user_info['id'];
        $data['gold'] = $gold;
        $data['withdraw_number'] = Random::numeric(17);
        
        Db::startTrans();
        $is_success = WithdrawModel::create($data);

        if($is_success){
            User::money((0-$gold), $user_info['id'], '申请提现');
            Db::commit();
            $with_info['id'] = $is_success['id'];
            $with_info['withdraw_number'] = $is_success['withdraw_number'];
            $this->success('提交成功', $with_info);
        }else{
            Db::rollback();
            $this->error('提交失败');
        }
    }
}
